package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.Resource;

public class ResourceDao {

	public static enum Mode {

		INSERT, UPDATE, DELETE

	}
	public void update(Object arg, Mode mode) {

		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();

			switch (mode) {

			case INSERT:
				smc.insert("resource.insertResource", arg);
				break;

			case UPDATE:
				smc.update("resource.updateResourceByRid", arg);
				break;

			case DELETE:
				smc.delete("resource.deleteResource", arg);
				break;

			}

			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	/*
	 * 修改文件夹或文件名称
	 */
	public void modifyNameByRid(int rid,String name,long last_modify_time){
		Resource resource=new Resource();
		resource.setRid(rid);
		resource.setName(name);
		resource.setLast_modify_time(last_modify_time);
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.modifyNameByRid", resource);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
	}
	/*
	 * 判断同一文件夹下是否同名
	 */
	public boolean isExists(int pid,String name){
		SqlMapClient smc = null;
		Resource resource=new Resource();
		resource.setPid(pid);
		resource.setName(name);
		boolean flag=false;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			int count=(Integer) smc.queryForObject("resource.isExists", resource);
			if(count>0)
				flag=true;
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return flag;
	}
	public List<Resource> getResourcesByUsername(String username) {
		SqlMapClient smc = null;
		List<Resource> Resources=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Resources =(List<Resource>) smc.queryForList("resource.getResourcesByUsername", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Resources;

	}
	/*
	 * 获取被删除的文件
	 */
	public List<Resource> getDropedResources(String owner) {
		SqlMapClient smc = null;
		List<Resource> resources=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			resources =(List<Resource>) smc.queryForList("resource.getDropedResources", owner);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return resources;

	}
	
	public void clearDash(String owner){
		Resource resource=new Resource();
		resource.setLast_modify_time(System.currentTimeMillis());
		resource.setOwner(owner);
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.clearDash", resource);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
	}
	public void restoreByRid(int rid) {
		Resource resource=new Resource();
		resource.setLast_modify_time(System.currentTimeMillis());
		resource.setRid(rid);
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.restoreByRid", resource);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		
	}
	public void deleteByRid(int rid) {
		Resource resource=new Resource();
		resource.setLast_modify_time(System.currentTimeMillis());
		resource.setRid(rid);
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.deleteByRid", resource);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		
	}
	public void restoreResourcesByPath(String path) {
		SqlMapClient smc = null;
		Map map=new HashMap();
		map.put("path", path);
		map.put("last_modify_time", System.currentTimeMillis());
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.restoreResourcesByPath", map);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		
	}
	public void deleteResourcesByPath(Map map) {
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("resource.deleteResourcesByPath", map);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		
	}
	public List<Resource> getResourcesByPid(int pid) {
		SqlMapClient smc = null;
		List<Resource> Resources=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Resources =(List<Resource>) smc.queryForList("resource.getResourcesByPid", pid);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Resources;

	}
	public List<Resource> getAllResources() {
		SqlMapClient smc = null;
		List<Resource> Resources=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Resources =(List<Resource>) smc.queryForList("resource.getAllResources", null);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Resources;

	}

	public Resource getResourceByRid(int id) {
		Resource Resource = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Resource = (Resource) smc.queryForObject("resource.getResourceByRid", id);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return Resource;

	}

	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {
		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
	
		ResourceDao dao = new ResourceDao();
		
		dao.clearDash("aaa");
		
		
		List<Resource> list=dao.getDropedResources("aaa");
		System.out.println(list.size());
		/*
		 //增
		resource.setName("one");
		resource.setDescription("hello");
		dao.update(resource, Mode.INSERT);
		
		
		
		//查
		resource=dao.getResourceByRid(1);
		System.out.println(resource.getName());
		
		
		
		//改
		resource.setRid(1);
		resource.setName("update");
		dao.update(resource, Mode.UPDATE);
		
		
		//删
		dao.update(1, Mode.DELETE);
		
		List list=dao.getAllResources();
		// List<Resource> list=dao.getResourcesByUsername("aaa");
		System.out.println(list.size());
		*/
	}

}